package com.rules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rules.admin.utils.ResultUtils;
import com.rules.admin.entity.SysGroupUserRole;
import com.rules.admin.service.SysGroupUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/groupUserRole"})
public class SysGroupUserRoleController {
   @Autowired
   private SysGroupUserRoleService sysGroupUserRoleService;

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addUser(@RequestBody SysGroupUserRole[] sysGroupUserRole) {
      this.sysGroupUserRoleService.insert(sysGroupUserRole);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<SysGroupUserRole> getByUserId(long userId) {

      return this.sysGroupUserRoleService.getByUserId(userId);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public void deleteById(@PathVariable("id") long userId) {

      this.sysGroupUserRoleService.deleteByUserId(userId);
   }

   @RequestMapping(
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteByUserId(SysGroupUserRole sysGroupUserRole) {
      LambdaQueryWrapper<SysGroupUserRole> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SysGroupUserRole::getUserId, sysGroupUserRole.getUserId()).eq(SysGroupUserRole::getGroupId, sysGroupUserRole.getGroupId());
      this.sysGroupUserRoleService.remove(queryWrapper);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/ids"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteUsers(long[] ids, long groupId) {
      this.sysGroupUserRoleService.deleteByIds(ids, groupId);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRole(@RequestBody SysGroupUserRole[] sysGroupUserRoles) {
      this.sysGroupUserRoleService.updateRole(sysGroupUserRoles);
      return ResultUtils.getSuccessResultData();
   }
}
